package com.chiaching.webmagic.repostory.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.chiaching.webmagic.domain.constant.ElasticsearchCommonConstant;
import com.chiaching.webmagic.domain.constant.ElasticsearchIndexConstant;
import com.chiaching.webmagic.domain.document.GoodDocument;
import com.chiaching.webmagic.repostory.GoodsRepostory;
import com.chiaching.webmagic.repostory.service.GoodsRepostoryService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @email 1738870995@qq.com
 * @author: 何佳靖
 * @date: 2022/3/26 12:08
 */
@Repository
@Slf4j
public class GoodsRepostoryServiceImpl implements GoodsRepostoryService {

    @Resource
    private GoodsRepostory goodsRepostory;

    @Resource
    private RestHighLevelClient restHighLevelClient;


    @Override
    public void addGoods(List<GoodDocument> goodRepostories) {
        try {
            goodsRepostory.saveAll(goodRepostories);
        } catch (Exception e) {
            log.error("商品插入es出现异常!",e);
        }
    }

    @Override
    public List<GoodDocument> searchGoodsByTitle(String title) {
        try {
            SearchRequest searchRequest = new SearchRequest(ElasticsearchIndexConstant.SUPER_MALL_GOOD);
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(QueryBuilders.matchQuery(GoodDocument.field_title,title));
            HighlightBuilder highlightBuilder = new HighlightBuilder();
            highlightBuilder.preTags(ElasticsearchCommonConstant.HIGHLIGHT_PRE_TAG);
            highlightBuilder.field(GoodDocument.field_title);
            highlightBuilder.postTags(ElasticsearchCommonConstant.HIGHLIGHT_POST_TAG);
            highlightBuilder.requireFieldMatch(false);
            searchSourceBuilder.highlighter(highlightBuilder);
            searchRequest.source(searchSourceBuilder);
            SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            SearchHit[] hits = search.getHits().getHits();
            return Stream.of(hits).map(param -> {
                GoodDocument goodDocument = BeanUtil.copyProperties(param.getSourceAsMap(), GoodDocument.class);
                for (String key : param.getHighlightFields().keySet()) {
                    if (Objects.equals(key,GoodDocument.field_title)){
                        goodDocument.setTitle(param.getHighlightFields().get(key).getFragments()[0].toString());
                    }
                }
                return goodDocument;
            }).collect(Collectors.toList());
        } catch (IOException e) {
            log.error("商品es模糊查询出现异常!",e);
        }
        return Collections.emptyList();
    }
}
